import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/',
    component: () => import('@/views/login/login'),
    hidden:true
  },
  {
    path: '/shopRegister',
    name: '商家入驻',
    component: () => import('@/views/org/shopRegister')
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/home',
    component: Layout,
    children: [{
      path: '',
      name: '首页',
      component: () => import('@/views/home/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  {
    path: '/org',
    component: Layout,
    redirect: '/org/tenantType',
    meta:{
      title:'组织机构管理',icon: 'el-icon-s-help'
    },
    children:[
      {
        path: 'department',
        name: '部门管理',
        component: () => import('@/views/org/department'),
        meta:{
          title:'部门管理',icon: 'table'
        }
      },
      {
        path: 'employee',
        name: '员工管理',
        component: () => import('@/views/org/employee'),
        meta:{
          title:'员工管理',icon: 'table'
        }
      }
      ,
      {
        path: 'shop',
        name: '店铺管理',
        component: () => import('@/views/org/shop'),
        meta:{
          title:'店铺管理',icon: 'table'
        }
      }
    ] 
  },
  {
    path: '/auth',
    component: Layout,
    redirect: '/auth/permission',
    meta:{
      title:'权限管理',icon: 'el-icon-s-help'
    },
    children:[
      {
        path: 'permission',
        name: '权限管理',
        component: () => import('@/views/auth/permission'),
        meta:{
          title:'权限管理',icon: 'table'
        }
      },
      {
        path: 'role',
        name: '角色管理',
        component: () => import('@/views/auth/role'),
        meta:{
          title:'角色管理',icon: 'table'
        }
      }
    ] 
  },
  {
    path: '/car',
    component: Layout,
    redirect: '/car/carType',
    meta:{
      title:'汽车管理',icon: 'el-icon-s-help'
    },
    children:[
      {
        path: 'carType',
        name: '汽车类型',
        component: () => import('@/views/car/carType'),
        meta:{
          title:'汽车类型',icon: 'table'
        }
      },
      {
        path: 'car',
        name: '汽车列表',
        component: () => import('@/views/car/car'),
        meta:{
          title:'汽车列表',icon: 'table'
        }
      },
    ] 
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/') {
    localStorage.removeItem('userInfo');
    localStorage.removeItem('token');
  }
  let user = JSON.parse(localStorage.getItem('userInfo'));
  if (!user && to.path != '/' && to.path != '/shopRegister') {
    next({ path: '/' })
  } else {
    next()
  }
})

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
